set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.26)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
#set(CMAKE_C_COMPILER_LAUNCHER ccache)

# project settings
project(at32f425_template C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_VERBOSE_MAKEFILE ON)

set(MCU -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=soft)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/project/misc/AT32F425x8_FLASH.ld)

add_compile_options(${MCU})
add_compile_options(-Wall -fdata-sections -ffunction-sections)
# Wno
add_compile_options(-Wno-missing-braces)
add_compile_options(-Wno-format)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    # flto
#    add_compile_options(-flto)
    add_compile_options(-O3)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    add_compile_options(-Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    add_compile_options(-Og -g -gdwarf-3)
endif ()

# CherryUSB
file(GLOB_RECURSE CHERRYUSB_SOURCES
        "components/CherryUSB/core/usbd_core.c"
        "components/CherryUSB/port/dwc2/usb_dc_dwc2.c"
        "components/CherryUSB/class/cdc/usbd_cdc.c")

include_directories("components/CherryUSB/common"
        "components/CherryUSB/core"
        "components/CherryUSB/class/cdc"
        "components/CherryUSB/class/msc")
# CherryUSB END
# CherryRB
file(GLOB_RECURSE CHERRYRB_SOURCES
        "components/CherryRB/chry_ringbuffer.c")

include_directories("components/CherryRB")
# CherryRB END
# CherryDAP
file(GLOB_RECURSE CHERRYDAP_SOURCES
        "components/CherryDAP/dap_main.c"
        "components/CherryDAP/DAP/Source/*.c")

include_directories("components/CherryDAP/DAP/Include"
        "components/CherryDAP")
# CherryDAP END
# SWD HOST
file(GLOB_RECURSE SWDHOST_SOURCES
        "components/SwdHost/swd_host.c")

include_directories("components/SwdHost")
# SWD HOST END

include_directories("project/inc"
        "project/board"
        "libraries/cmsis/cm4/core_support"
        "libraries/cmsis/cm4/device_support"
        "libraries/drivers/inc")

add_definitions(-DAT_START_F425_V1
        -DAT32F425F8P7
        -DUSE_STDPERIPH_DRIVER
        -DCONFIG_USB_DWC2_PORT=FS_PORT)

file(GLOB_RECURSE SOURCES
        "project/src/*.c"
        "project/board/*.c"
        "libraries/cmsis/cm4/device_support/system_at32f425.c"
        "libraries/drivers/src/*.c"
        "project/src/startup_at32f425.s"
        ${CHERRYUSB_SOURCES}
        ${CHERRYRB_SOURCES}
        ${CHERRYDAP_SOURCES}
        ${SWDHOST_SOURCES})

add_link_options(${MCU})
add_link_options(--specs=nosys.specs)
add_link_options(-T ${LINKER_SCRIPT})
add_link_options(-lc -lm)
add_link_options(-Wl,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections)

add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}.elf>
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
